{% set ne_languages = ['ar', 'bn', 'de', 'el', 'en', 'es', 'fa', 'fr', 'he', 'hi', 'hu', 'id', 'it', 'ja', 'ko', 'nl', 'pl', 'pt', 'ru', 'sv', 'tr', 'uk', 'ur', 'vi', 'zh', 'zht'] %}
{% set ne_viewpoints = ['iso', 'ar', 'bd', 'br', 'cn', 'de', 'eg', 'es', 'fr', 'gb', 'gr', 'id', 'il', 'in', 'it', 'jp', 'ko', 'ma', 'nl', 'np', 'pk', 'pl', 'ps', 'pt', 'ru', 'sa', 'se', 'tr', 'tw', 'ua', 'us', 'vn'] %}

{% macro ne_common_properties() %}

  -- common properties
  jsonb_build_object(
    'source', 'naturalearthdata.com'
  ) AS __properties__

{% endmacro %}

{% macro ne_boundaries_query(name, viewpoints, table) %}

SELECT

  gid AS __id__,

  {% filter geometry %}the_geom{% endfilter %} AS __geometry__,
  NULL::bytea AS __label__,

  {{ ne_common_properties() }},

  jsonb_build_object(
{% if name %}
    'name', name,
{% endif %}
{% if viewpoints %}
  {%- for vpt in ne_viewpoints %}
    'fclass_{{vpt}}', fclass_{{vpt}},
  {%- endfor %}
{% endif %}
    'min_zoom', mz_boundary_min_zoom,
    'featurecla', featurecla,
    'scalerank', scalerank
  ) AS __boundaries_properties__,

  NULL::jsonb AS __earth_properties__,
  NULL::jsonb AS __landuse_properties__,
  NULL::jsonb AS __places_properties__,
  NULL::jsonb AS __roads_properties__,
  NULL::jsonb AS __water_properties__

FROM
  {{ table }}

WHERE
  {{ bounds['line']|bbox_filter('the_geom', 3857) }} AND
  mz_boundary_min_zoom < {{ zoom + 1 }}

{% endmacro %}

{% macro ne_earth_query(table) %}

SELECT

  gid AS __id__,

  {% filter geometry %}{{ bounds['polygon']|bbox_intersection('the_geom') }}{% endfilter %} AS __geometry__,
  {% filter geometry %}mz_label_placement{% endfilter %} AS __label__,

  {{ ne_common_properties() }},

  NULL::jsonb AS __boundaries_properties__,

  jsonb_build_object(
    'min_zoom', mz_earth_min_zoom
  ) AS __earth_properties__,

  NULL::jsonb AS __landuse_properties__,
  NULL::jsonb AS __places_properties__,
  NULL::jsonb AS __roads_properties__,
  NULL::jsonb AS __water_properties__

FROM {{ table }}

WHERE
  mz_earth_min_zoom < {{ zoom + 1 }} AND
  {{ bounds['polygon']|bbox_filter('the_geom', 3857) }}

{% endmacro %}

{% macro ne_landuse_query(table) %}

SELECT

  gid AS __id__,

  {% filter geometry %}{{ bounds['polygon']|bbox_intersection('the_geom') }}{% endfilter %} AS __geometry__,
  {% filter geometry %}mz_label_placement{% endfilter %} AS __label__,

  {{ ne_common_properties() }},

  NULL::jsonb AS __boundaries_properties__,
  NULL::jsonb AS __earth_properties__,

  jsonb_build_object(
    'area', way_area,
    'kind', 'urban_area'
  ) AS __landuse_properties__,

  NULL::jsonb AS __places_properties__,
  NULL::jsonb AS __roads_properties__,
  NULL::jsonb AS __water_properties__

FROM {{ table }}

WHERE
  {{ bounds['polygon']|bbox_filter('the_geom', 3857) }} AND
  mz_landuse_min_zoom < {{ zoom + 1 }}

{% endmacro %}

{% macro ne_water_query(boundary_geometry, label_geometry, name, boundary, has_area, table, wikidata=False) %}

SELECT

  gid AS __id__,

{% if boundary_geometry %}
  {% filter geometry %}st_boundary(the_geom){% endfilter %} AS __geometry__,
{% else %}
  {% filter geometry %}{{ bounds['polygon']|bbox_padded_intersection('the_geom') }}{% endfilter %} AS __geometry__,
{% endif %}
{% if label_geometry %}
  {% filter geometry %}mz_label_placement{% endfilter %} AS __label__,
{% else %}
  NULL::bytea AS __label__,
{% endif %}

  {{ ne_common_properties() }},

  NULL::jsonb AS __boundaries_properties__,
  NULL::jsonb AS __earth_properties__,
  NULL::jsonb AS __landuse_properties__,
  NULL::jsonb AS __places_properties__,
  NULL::jsonb AS __roads_properties__,

  jsonb_build_object(
{% if name %}
    'name', name,
    {%- for lang in ne_languages %}
    'name_{{lang}}', name_{{lang}},
    {%- endfor %}
{% endif %}
{% if boundary %}
    'boundary', true,
{% endif %}
{% if has_area %}
    'area', way_area,
{% endif %}
{% if wikidata %}
    'wikidata', wikidataid,
{% endif %}
    'min_zoom', mz_water_min_zoom,
    'featurecla', featurecla
  ) AS __water_properties__

FROM {{ table }}

WHERE
  mz_water_min_zoom < {{ zoom + 1 }} AND
  {{ bounds['polygon']|bbox_filter('the_geom', 3857) }}

{% endmacro %}

{% block query %}
{% endblock %}
